* This file creates the XC working dataset used Kinder and Ryan, "Prejudice and Politics Re-examined"
* Analysis was conducted on State/SE 13.1 for Mac (64-bit Intel) 

* Load raw data
use "anes2008xs.dta", clear

*Weights
gen preweight = V080101
gen postweight = V080102

* Race
gen white = 0
replace white = 1 if (V081103a==21 | V081103a==25)
gen black = 0
replace black = 1 if (V081103a==22 | V081103a==26)
gen hispanic = 0
replace hispanic = 1 if (V081103a<21)
gen otherrace = 0
replace otherrace = 1 if white==0 & black==0 & hispanic==0

* Education
gen educ = .
replace educ = 0 if V083218x==1 | V083218x==2
replace educ = 1 if V083218x==3 
replace educ = 2 if V083218x==4 | V083218x==5
replace educ = 3 if V083218x==6
replace educ = 4 if V083218x==7

lab def educ 0 "No Diploma" 1 "Diploma only" 2 "Some college" 3 "BA" 4 "Adv. Degre"
lab val educ educ

* Age
gen age2 = V081104
gen age = (age2-17)/73
gen agebin = .
replace agebin = 0 if age2<30
replace agebin = 1 if age2>=30 & age2<=39
replace agebin = 2 if age2>=40 & age2<=49
replace agebin = 3 if age2>=50 & age2<=59
replace agebin = 4 if age2>=60

* Gender
gen female = 0
replace female = 1 if V081101==2

* Region
gen region = .
replace region = 0 if V081204==1
replace region = 1 if V081204==2
replace region = 2 if V081204==3
replace region = 3 if V081204==4

* Party ID
gen pidr = .
recode pidr (. = 0) if V083098x==0
recode pidr (. = .25) if V083098x==1
recode pidr (. = .25) if V083098x==2
recode pidr (. = .5) if V083098x==3
recode pidr (. = .75) if V083098x==4
recode pidr (. = .75) if V083098x==5
recode pidr (. = 1) if V083098x==6

* PID Strength
gen dempidstr = .
recode dempidstr (. = 0) if V083098x==3
recode dempidstr (. = .5) if V083098x==2 | V083098x==1
recode dempidstr (. = 1) if V083098x==0

gen reppidstr = .
recode reppidstr (. = 0) if V083098x==3
recode reppidstr (. = .5) if V083098x==4 | V083098x==5
recode reppidstr (. = 1) if V083098x==6

gen pidstr = max(dempidstr, reppidstr)

* Racial resentment
alpha V085143 V085144 V085145 V085146, gen(rresent) c
alpha V085143 V085144 V085145 V085146 if white==1 // .77
replace rresent = (((rresent + 2)/4)*2)-1

* Stereotyping
gen blazydif = (6 - V083207a + V083207b)/12
gen buninteldif = (6 - V083208a + V083208b)/12
alpha blazydif buninteldif, gen(stereotype) c

gen stereo2 = (stereotype*2)-1

* General election vote
gen obvote = .
replace obvote = 1 if V085044a==1
replace obvote = 0 if V085044a==3
gen mcvote = 1-obvote

* Primary vote
gen primvote = .
replace primvote = 1 if V083077a!=.
replace primvote = 0 if V083077a==12

* Gov. Assistance
gen govassist = (V083137-1)/6

* Hire blacks
gen hireblacks2 = .
replace hireblacks2 = 3 if V085157b==1
replace hireblacks2 = 2 if V085157b==5
replace hireblacks2 = 1 if V085157a==5
replace hireblacks2 = 0 if V085157a==1
replace hireblacks2 = 2 if V085157==5 & hireblacks2==.
replace hireblacks2 = 1 if V085157==1 & hireblacks2==.
gen hireblacks = hireblacks2 / 3

* Affect misattribution procedure
egen blackresps = rownonmiss(V085311 V085312 V085313 V085314 V085315 V085316 V085317 V085318 V085319 V085320 V085321 V085322 V085323 V085324 V085325 V085326 V085327 V085328  V085329 V085330 V085331 V085332 V085333 V085334)
egen bpleas = anycount(V085311 V085312 V085313 V085314 V085315 V085316 V085317 V085318 V085319 V085320 V085321 V085322 V085323 V085324 V085325 V085326 V085327 V085328  V085329 V085330 V085331 V085332 V085333 V085334), values(1)
replace bpleas = . if V085334==. // Didn't complete procedure

egen bunpleas = anycount(V085311 V085312 V085313 V085314 V085315 V085316 V085317 V085318 V085319 V085320 V085321 V085322 V085323 V085324 V085325 V085326 V085327 V085328  V085329 V085330 V085331 V085332 V085333 V085334), values(0)
replace bunpleas = . if V085334==.

egen whiteresps = rownonmiss(V085335 V085336 V085337 V085338 V085339 V085340 V085341 V085342 V085343 V085344 V085345 V085346 V085347 V085348 V085349 V085350 V085351 V085352 V085353 V085354 V085355 V085356 V085357 V085358) 
egen wpleas = anycount(V085335 V085336 V085337 V085338 V085339 V085340 V085341 V085342 V085343 V085344 V085345 V085346 V085347 V085348 V085349 V085350 V085351 V085352 V085353 V085354 V085355 V085356 V085357 V085358), values(1) 
replace wpleas = . if V085358==.

egen wunpleas = anycount(V085335 V085336 V085337 V085338 V085339 V085340 V085341 V085342 V085343 V085344 V085345 V085346 V085347 V085348 V085349 V085350 V085351 V085352 V085353 V085354 V085355 V085356 V085357 V085358), values(0) 
replace wunpleas = . if V085358==.

gen propwpleas = wpleas / whiteresps
gen propbpleas = bpleas / blackresps
gen wbdif = propwpleas - propbpleas

* Identify respondents with all positive or all negative AMP responses
gen allpos = 0
replace allpos = 1 if propwpleas==1 & propbpleas==1
gen allneg = 0
replace allneg = 1 if propwpleas==0 & propbpleas==0

* Trim extreme responses from AMP
gen wbdif2 = wbdif
replace wbdif2 = -.5 if wbdif2<-.5

* Reliability. The remainder of the file is all to calculate reliability of the AMP.
* The procedure is to create randomly assigned pairs of black and white AMP 
* items--the random assignment was done externally--and code the pairs as being prejudiced,
* reverse-prejudiced, or inconsistent. Then, you calculate an alpha coefficient across 
* these pairs. (Line 24 of the analysis file.)

rename V085311 bamp1
rename V085312 bamp2
rename V085313 bamp3
rename V085314 bamp4
rename V085315 bamp5
rename V085316 bamp6
rename V085317 bamp7
rename V085318 bamp8
rename V085319 bamp9
rename V085320 bamp10
rename V085321 bamp11
rename V085322 bamp12
rename V085323 bamp13
rename V085324 bamp14
rename V085325 bamp15
rename V085326 bamp16
rename V085327 bamp17
rename V085328 bamp18
rename V085329 bamp19
rename V085330 bamp20
rename V085331 bamp21
rename V085332 bamp22
rename V085333 bamp23
rename V085334 bamp24

rename V085335 wamp1
rename V085336 wamp2
rename V085337 wamp3
rename V085338 wamp4
rename V085339 wamp5
rename V085340 wamp6
rename V085341 wamp7
rename V085342 wamp8
rename V085343 wamp9
rename V085344 wamp10
rename V085345 wamp11
rename V085346 wamp12
rename V085347 wamp13
rename V085348 wamp14
rename V085349 wamp15
rename V085350 wamp16
rename V085351 wamp17
rename V085352 wamp18
rename V085353 wamp19
rename V085354 wamp20
rename V085355 wamp21
rename V085356 wamp22
rename V085357 wamp23
rename V085358 wamp24

* Create pairs of AMP items
gen ampitem2 = .
replace ampitem2 = 1 if (bamp2==0 & wamp21==1)
replace ampitem2 = 0 if (bamp2==0 & wamp21==0)
replace ampitem2 = 0 if (bamp2==1 & wamp21==1)
replace ampitem2 = -1 if (bamp2==1 & wamp21==0)

gen ampitem3 = .
replace ampitem3 = 1 if (bamp3==0 & wamp5==1)
replace ampitem3 = 0 if (bamp3==0 & wamp5==0)
replace ampitem3 = 0 if (bamp3==1 & wamp5==1)
replace ampitem3 = -1 if (bamp3==1 & wamp5==0)

gen ampitem4 = .
replace ampitem4 = 1 if (bamp4==0 & wamp24==1)
replace ampitem4 = 0 if (bamp4==0 & wamp24==0)
replace ampitem4 = 0 if (bamp4==1 & wamp24==1)
replace ampitem4 = -1 if (bamp4==1 & wamp24==0)

gen ampitem5 = .
replace ampitem5 = 1 if (bamp5==0 & wamp2==1)
replace ampitem5 = 0 if (bamp5==0 & wamp2==0)
replace ampitem5 = 0 if (bamp5==1 & wamp2==1)
replace ampitem5 = -1 if (bamp5==1 & wamp2==0)

gen ampitem6 = .
replace ampitem6 = 1 if (bamp6==0 & wamp3==1)
replace ampitem6 = 0 if (bamp6==0 & wamp3==0)
replace ampitem6 = 0 if (bamp6==1 & wamp3==1)
replace ampitem6 = -1 if (bamp6==1 & wamp3==0)

gen ampitem7 = .
replace ampitem7 = 1 if (bamp7==0 & wamp11==1)
replace ampitem7 = 0 if (bamp7==0 & wamp11==0)
replace ampitem7 = 0 if (bamp7==1 & wamp11==1)
replace ampitem7 = -1 if (bamp7==1 & wamp11==0)

gen ampitem8 = .
replace ampitem8 = 1 if (bamp8==0 & wamp12==1)
replace ampitem8 = 0 if (bamp8==0 & wamp12==0)
replace ampitem8 = 0 if (bamp8==1 & wamp12==1)
replace ampitem8 = -1 if (bamp8==1 & wamp12==0)

gen ampitem9 = .
replace ampitem9 = 1 if (bamp9==0 & wamp14==1)
replace ampitem9 = 0 if (bamp9==0 & wamp14==0)
replace ampitem9 = 0 if (bamp9==1 & wamp14==1)
replace ampitem9 = -1 if (bamp9==1 & wamp14==0)

gen ampitem10 = .
replace ampitem10 = 1 if (bamp10==0 & wamp20==1)
replace ampitem10 = 0 if (bamp10==0 & wamp20==0)
replace ampitem10 = 0 if (bamp10==1 & wamp20==1)
replace ampitem10 = -1 if (bamp10==1 & wamp20==0)

gen ampitem11 = .
replace ampitem11 = 1 if (bamp11==0 & wamp17==1)
replace ampitem11 = 0 if (bamp11==0 & wamp17==0)
replace ampitem11 = 0 if (bamp11==1 & wamp17==1)
replace ampitem11 = -1 if (bamp11==1 & wamp17==0)

gen ampitem12 = .
replace ampitem12 = 1 if (bamp12==0 & wamp19==1)
replace ampitem12 = 0 if (bamp12==0 & wamp19==0)
replace ampitem12 = 0 if (bamp12==1 & wamp19==1)
replace ampitem12 = -1 if (bamp12==1 & wamp19==0)

gen ampitem13 = .
replace ampitem13 = 1 if (bamp13==0 & wamp23==1)
replace ampitem13 = 0 if (bamp13==0 & wamp23==0)
replace ampitem13 = 0 if (bamp13==1 & wamp23==1)
replace ampitem13 = -1 if (bamp13==1 & wamp23==0)

gen ampitem14 = .
replace ampitem14 = 1 if (bamp14==0 & wamp8==1)
replace ampitem14 = 0 if (bamp14==0 & wamp8==0)
replace ampitem14 = 0 if (bamp14==1 & wamp8==1)
replace ampitem14 = -1 if (bamp14==1 & wamp8==0)

gen ampitem15 = .
replace ampitem15 = 1 if (bamp15==0 & wamp4==1)
replace ampitem15 = 0 if (bamp15==0 & wamp4==0)
replace ampitem15 = 0 if (bamp15==1 & wamp4==1)
replace ampitem15 = -1 if (bamp15==1 & wamp4==0)

gen ampitem16 = .
replace ampitem16 = 1 if (bamp16==0 & wamp16==1)
replace ampitem16 = 0 if (bamp16==0 & wamp16==0)
replace ampitem16 = 0 if (bamp16==1 & wamp16==1)
replace ampitem16 = -1 if (bamp16==1 & wamp16==0)

gen ampitem17 = .
replace ampitem17 = 1 if (bamp17==0 & wamp22==1)
replace ampitem17 = 0 if (bamp17==0 & wamp22==0)
replace ampitem17 = 0 if (bamp17==1 & wamp22==1)
replace ampitem17 = -1 if (bamp17==1 & wamp22==0)

gen ampitem18 = .
replace ampitem18 = 1 if (bamp18==0 & wamp15==1)
replace ampitem18 = 0 if (bamp18==0 & wamp15==0)
replace ampitem18 = 0 if (bamp18==1 & wamp15==1)
replace ampitem18 = -1 if (bamp18==1 & wamp15==0)

gen ampitem19 = .
replace ampitem19 = 1 if (bamp19==0 & wamp7==1)
replace ampitem19 = 0 if (bamp19==0 & wamp7==0)
replace ampitem19 = 0 if (bamp19==1 & wamp7==1)
replace ampitem19 = -1 if (bamp19==1 & wamp7==0)

gen ampitem20 = .
replace ampitem20 = 1 if (bamp20==0 & wamp10==1)
replace ampitem20 = 0 if (bamp20==0 & wamp10==0)
replace ampitem20 = 0 if (bamp20==1 & wamp10==1)
replace ampitem20 = -1 if (bamp20==1 & wamp10==0)

gen ampitem21 = .
replace ampitem21 = 1 if (bamp21==0 & wamp13==1)
replace ampitem21 = 0 if (bamp21==0 & wamp13==0)
replace ampitem21 = 0 if (bamp21==1 & wamp13==1)
replace ampitem21 = -1 if (bamp21==1 & wamp13==0)

gen ampitem22 = .
replace ampitem22 = 1 if (bamp22==0 & wamp9==1)
replace ampitem22 = 0 if (bamp22==0 & wamp9==0)
replace ampitem22 = 0 if (bamp22==1 & wamp9==1)
replace ampitem22 = -1 if (bamp22==1 & wamp9==0)

gen ampitem23 = .
replace ampitem23 = 1 if (bamp23==0 & wamp18==1)
replace ampitem23 = 0 if (bamp23==0 & wamp18==0)
replace ampitem23 = 0 if (bamp23==1 & wamp18==1)
replace ampitem23 = -1 if (bamp23==1 & wamp18==0)

gen ampitem24 = .
replace ampitem24 = 1 if (bamp24==0 & wamp6==1)
replace ampitem24 = 0 if (bamp24==0 & wamp6==0)
replace ampitem24 = 0 if (bamp24==1 & wamp6==1)
replace ampitem24 = -1 if (bamp24==1 & wamp6==0)

save "xs_working.dta", replace
